﻿Imports System.Data.SqlClient
Public Class frmThietLap
    Dim frmParent As frmCenter
    Sub New(Optional ByRef parent As frmCenter = Nothing)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        frmParent = parent
    End Sub
    Private Sub frmThietLap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.txtTenCT.Text = modKetNoi.aryConfig("CFG01")
        Me.txtDiaChi.Text = modKetNoi.aryConfig("CFG02")
        Me.txtSoDt.Text = modKetNoi.aryConfig("CFG03")
        Me.txtMSThue.Text = modKetNoi.aryConfig("CFG04")
        Me.txtTKNganHang.Text = modKetNoi.aryConfig("CFG05")
        Me.txtGiamDoc.Text = modKetNoi.aryConfig("CFG06")
        Me.txtKeToan.Text = modKetNoi.aryConfig("CFG07")
        Me.txtThuTruong.Text = modKetNoi.aryConfig("CFG08")
        Me.txtNguoiLap.Text = modKetNoi.aryConfig("CFG09")
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        If validateInput() Then
            If CapNhat() Then
                If Not frmParent Is Nothing Then
                    frmParent.btnCty.Text = modKetNoi.aryConfig("CFG01")
                End If
                MsgBox("Cập nhật thành công.")
            Else
                MsgBox("Cập nhật thất bại.")
            End If
        End If

    End Sub

    Private Sub btnHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub txtTenCT_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTenCT.Enter
        txtTenCT.BackColor = System.Drawing.SystemColors.Window
    End Sub

    Private Sub txtTenCT_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTenCT.Leave
        Me.txtTenCT.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
    End Sub

    Private Function CapNhat() As Boolean
        Dim strSql As String = ""
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Try
            myConn.Open()
            myComm.Connection = myConn
            strSql = "Update ThietLap set GiaTri = @GiaTri where TuKhoa = @TuKhoa"
            myComm.CommandText = strSql
            updateValue(txtTenCT.Text, "CFG01", myComm)
            updateValue(txtDiaChi.Text, "CFG02", myComm)
            updateValue(txtSoDt.Text, "CFG03", myComm)
            updateValue(txtMSThue.Text, "CFG04", myComm)
            updateValue(txtTKNganHang.Text, "CFG05", myComm)
            updateValue(txtGiamDoc.Text, "CFG06", myComm)
            updateValue(txtKeToan.Text, "CFG07", myComm)
            updateValue(txtThuTruong.Text, "CFG08", myComm)
            updateValue(txtNguoiLap.Text, "CFG09", myComm)

        Catch ex As Exception
            Return False
        End Try
        myConn.Close()
        myConn.Dispose()
        Return True
    End Function

    Private Sub updateValue(ByVal value As String, ByVal key As String, ByVal mycomm As SqlCommand)
        value = value.Trim()
        If value <> "" AndAlso value <> modKetNoi.aryConfig(key) Then
            mycomm.Parameters.Clear()
            mycomm.Parameters.AddWithValue("@TuKhoa", key)
            mycomm.Parameters.AddWithValue("@GiaTri", value)
            mycomm.ExecuteNonQuery()
            modKetNoi.aryConfig(key) = value
        End If
    End Sub

    Private Function validateInput() As Boolean
        If Me.txtTenCT.Text.Trim() = "" Then
            MsgBox("Vui lòng nhập tên công ty.")
            Me.txtTenCT.Focus()
            Return False
        End If
        Return True
    End Function
End Class